﻿// <copyright file="CloseableTabItem.cs" company="Soumya Chattopadhyay">
// Copyright (c) 2010 All Right Reserved
// </copyright>
// <disclaimer> This software is intented for educational purposes only and not for 
// commerical or prodution use. The software is provided "as is", without warranty 
// of any kind, express or implied, including but not limited to the warranties of 
// merchantability, fitness for a particular purpose and noninfringement. In no event 
// shall the author(s) or copyright holder(s) be liable for any claim, damages or other 
// liability, whether in an action of contract, tort or otherwise, arising from, out of 
// or in connection with the software or the use or other dealings in the software.
// </disclaimer>

namespace My.CodePlex.TFSExplorer.Controls
{
    using System;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Markup;
    using System.Windows.Media.Imaging;
    using System.Windows.Shapes;

    /// <summary>
    /// The closeable tab item
    /// </summary>
    public class CloseableTabItem : TabItem
    {
        #region Fields

        /// <summary>
        /// The close tab routed event
        /// </summary>
        public static readonly RoutedEvent CloseTabEvent = EventManager.RegisterRoutedEvent("CloseTab", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(CloseableTabItem));

        #endregion

        #region Constructor

        /// <summary>
        /// Initializes a new instance of the CloseableTabItem class
        /// </summary>
        public CloseableTabItem()
        {
            this.Loaded += this.ModifiedTabControl_Loaded;
        }

        #endregion

        #region Events

        /// <summary>
        /// Gets or sets the close tab routed event
        /// </summary>
        public event RoutedEventHandler CloseTab
        {
            add { AddHandler(CloseTabEvent, value); }
            remove { RemoveHandler(CloseTabEvent, value); }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// On loaded, we'll change the tab header
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The event arguments</param>
        private void ModifiedTabControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            // Create the button
            FrameworkElementFactory button = new FrameworkElementFactory(typeof(Button));
            button.SetValue(Button.HorizontalAlignmentProperty, HorizontalAlignment.Center);
            button.SetValue(Button.VerticalAlignmentProperty, VerticalAlignment.Center);
            button.SetValue(Button.WidthProperty, 12.0);
            button.SetValue(Button.HeightProperty, 12.0);
            button.SetValue(Button.ToolTipProperty, new ToolTip() { Content = "Close" });
            button.AddHandler(Button.ClickEvent, new RoutedEventHandler(this.CloseClick));

            ////StringBuilder sb = new StringBuilder();
            ////sb.Append("<Path xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" Stretch=\"Fill\" StrokeThickness=\"0.5\" Stroke=\"#FF333333\" Fill=\"#FF969696\" Data=\"F1 M 2.28484e-007,1.33331L 1.33333,0L 4.00001,2.66669L 6.66667,6.10352e-005L 8,1.33331L 5.33334,4L 8,6.66669L 6.66667,8L 4,5.33331L 1.33333,8L 1.086e-007,6.66669L 2.66667,4L 2.28484e-007,1.33331 Z \" HorizontalAlignment=\"Stretch\" VerticalAlignment=\"Stretch\"/>");
            ////Path p = (Path)XamlReader.Parse(sb.ToString());
            ////FrameworkElementFactory pth = new FrameworkElementFactory(typeof(Path));
            ////pth.SetValue(Path.DataProperty, p.GetValue(Path.DataProperty));
            ////pth.SetValue(Path.StrokeThicknessProperty, 0.5);

            ControlTemplate controlTemplate = (ControlTemplate)Application.Current.Resources["MyImageColumnTemplate"];
            button.SetValue(Button.TemplateProperty, controlTemplate);

            FrameworkElementFactory textBlock = new FrameworkElementFactory(typeof(TextBlock));
            textBlock.SetValue(Button.MarginProperty, new Thickness(4, 0, 4, 0));
            textBlock.SetValue(TextBlock.TextProperty, this.Header);

            FrameworkElementFactory dockPanel = new FrameworkElementFactory(typeof(DockPanel));
            dockPanel.AppendChild(textBlock);
            dockPanel.AppendChild(button);

            DataTemplate dataTemplate = new DataTemplate();
            dataTemplate.VisualTree = dockPanel;

            this.HeaderTemplate = dataTemplate;
        }

        /// <summary>
        /// Close the tab item
        /// </summary>
        /// <param name="sender">The sender object</param>
        /// <param name="e">The event arguments</param>
        private void CloseClick(object sender, System.Windows.RoutedEventArgs e)
        {
            this.RaiseEvent(new RoutedEventArgs(CloseTabEvent, this));
        }

        #endregion
    }
}
